package org.dtrd.modules.evaluation.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.evaluation.entity.bean.SdsInfo;
import org.dtrd.modules.evaluation.entity.dto.SdsDTO;
import org.dtrd.modules.evaluation.entity.po.DtrdEntEvaluationRdSds;
import org.dtrd.modules.evaluation.mapper.DtrdEntEvaluationRdSdsMapper;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdSdsService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.StringJoiner;

/**
 * <p>
 * 抑郁评估（SDS） 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-09-26
 */
@Service
@DS("multi-datasource1")
public class DtrdEntEvaluationRdSdsServiceImpl extends ServiceImpl<DtrdEntEvaluationRdSdsMapper, DtrdEntEvaluationRdSds> implements IDtrdEntEvaluationRdSdsService {

    @Override
    public SdsInfo saveSasInfo(SdsDTO dto) {
        StringJoiner joiner = new StringJoiner(",");
        int scoreTotal = 0;
        int scoreStandard = 0;
        int tmpScore = 0;
        for (int i = 0; i < dto.getAnswers().length; i++) {
            tmpScore = dto.getAnswers()[i];
            joiner.add(tmpScore+"");
            if (2 == i + 1 || 5 == i + 1 || 6 == i + 1 || 11 == i + 1 || 12 == i + 1 || 14 == i + 1 || 16 == i + 1 || 17 == i + 1 || 18 == i + 1
                    || 20 == i + 1) {
                tmpScore = 5 - tmpScore;
            }
            scoreTotal += tmpScore;
        }
        scoreStandard = (int) (scoreTotal * 1.25);
        float severity = BigDecimal.valueOf((float) scoreTotal / 80).setScale(2, RoundingMode.HALF_UP).floatValue();
        String output = "";
        if (severity < 0.5) {
            output = "您的抑郁指数低于0.5，没有抑郁症状。";
        } else if (severity >= 0.5 && severity <= 0.59) {
            output = "您的抑郁指数高于0.5，属于轻微至轻度抑郁。";
        } else if (severity >= 0.6 && severity <= 0.69) {
            output = "您的抑郁指数高于0.6，属于中至重度抑郁。";
        } else if (severity >= 0.7) {
            output = "您的抑郁指数高于0.7，属于重度抑郁。";
        }
        DtrdEntEvaluationRdSds sds = new DtrdEntEvaluationRdSds();
        sds.setUserType(dto.getUserType());
        sds.setCreateTime(new Date());
        sds.setScoreStandard(scoreStandard);
        sds.setScoreTotal(scoreTotal);
        sds.setOutput(output);
        sds.setInput(joiner.toString());
        if (dto.getPatientId() != null) {
            sds.setPatientId(dto.getPatientId());
            save(sds);
        }
        return new SdsInfo().parseFromPo(sds);
    }

    @Override
    public SdsInfo getSasInfoLatest(Integer patientId) {
        LambdaQueryWrapper<DtrdEntEvaluationRdSds> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(DtrdEntEvaluationRdSds::getPatientId, patientId)
                .eq(DtrdEntEvaluationRdSds::getIsDel, 0)
                .orderByDesc(DtrdEntEvaluationRdSds::getCreateTime)
                .last("limit 1");
        DtrdEntEvaluationRdSds sds = baseMapper.selectOne(lambdaQueryWrapper);
        SdsInfo sdsInfo = new SdsInfo();
        if (sds != null) {
            sdsInfo = sdsInfo.parseFromPo(sds);
        }
        return sdsInfo;
    }
}
